package com.gdones.wms.bus.classification.service.impl;

import cn.hutool.extra.pinyin.PinyinUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gdones.common.core.dto.MyResult;
import com.gdones.common.core.util.MyPinYinUtil;
import com.gdones.wms.bus.classification.dto.WmsClassification;
import com.gdones.wms.bus.classification.mapper.WmsClassificationMapper;
import com.gdones.wms.bus.classification.service.IWmsClassificationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gdones.wms.core.util.MySysUtil;
import net.sourceforge.pinyin4j.PinyinHelper;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 物资分类主表 服务实现类
 * </p>
 *
 * @author GaoJingBo
 * @since 2023-05-04
 */
@Service
public class WmsClassificationServiceImpl extends ServiceImpl<WmsClassificationMapper, WmsClassification> implements IWmsClassificationService {

    @Override
    public MyResult getList(WmsClassification classification) {

        MyResult result = new MyResult();

        List<WmsClassification> list = list();

        result.setMsg("查询成功");
        result.setData(list);

        return result;
    }

    @Override
    public MyResult add(WmsClassification classification) {

        MyResult result = new MyResult();

        String code = "MRO-" + MyPinYinUtil.getPinYinByString(classification.getName()) + "-" + new Date().getTime();

        classification.setCode(code);

        String innerCode = MySysUtil.getInnerCode("t_wms_classification", "classificationID", classification.getParentID());
        classification.setInnerCode(innerCode);


        boolean save = save(classification);
        if(save){
            result.setMsg("添加分类成功！");
        }else{
            result.setMsg("添加分类失败！");
            result.setCode("9999");
        }

        return result;
    }

    @Override
    public MyResult update(WmsClassification classification) {

        MyResult result = new MyResult();

        WmsClassificationMapper classificationMapper = getBaseMapper();

        WmsClassification old = getById(classification.getClassificationID());

        // 父级节点发生变化
        if(!(old.getParentID().equals(classification.getParentID()))){

            // 生成新的内部编码
            String innerCode = MySysUtil.getInnerCode("t_wms_classification", "classificationID", classification.getParentID());
            classification.setInnerCode(innerCode);

            boolean b = updateById(classification);

            // 修改子集数据
            int i = classificationMapper.updateSonsInnerCode(old.getInnerCode(),innerCode);

            if(b){
                result.setMsg("修改成功！");
            }else{
                result.setMsg("修改失败！");
                result.setCode("9999");
            }

        }else{// 父级节点没有发生变化
            boolean b = updateById(classification);
            if(b){
                result.setMsg("修改分类成功！");
            }else{
                result.setMsg("修改分类失败！");
                result.setCode("9999");
            }
        }

        return result;
    }

    @Override
    public MyResult deletes(String ids) {

        MyResult result = new MyResult();

        QueryWrapper<WmsClassification> queryWrapper = new QueryWrapper<>();
        queryWrapper.likeRight("innerCode",ids);

        WmsClassificationMapper classificationMapper = getBaseMapper();
        int delete = classificationMapper.delete(queryWrapper);

        if(delete>0){
            result.setMsg("删除成功！");
        }else{
            result.setMsg("删除失败！");
            result.setCode("9999");
        }

        return result;
    }
}
